//
//  CategoryDetailListViewController.m
//  cookBook
//
//  Created by xalo on 16/1/25.
//  Copyright © 2016年 张广斌. All rights reserved.
//

#import "CategoryDetailListViewController.h"
#import "CategoryHelper.h"
#import "CategoryDetailViewCell.h"
#import "CategoryModel.h"
#import "WebViewController.h"

@interface CategoryDetailListViewController ()
@property (nonatomic,strong) NSArray * modelArray;
@property (nonatomic,weak) CategoryDetailListViewController *WeakSelf;
@property (nonatomic,assign) BOOL isBlank;
@end

//控制页数
static NSInteger i = 1;

@implementation CategoryDetailListViewController


- (void)viewDidLoad {
    self.isBlank = NO;
    
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.title = self.titleString;

    //清空之前加载过的数据
    [[CategoryHelper sharedManage].modelArray removeAllObjects];
    
    //请求当前页数据
    _WeakSelf = self;
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [[CategoryHelper sharedManage] fetchModelWithEffectOrCategory:self.categoryOrEffect Page:i Title:self.titleString block:^{
        
        if (self.modelArray.count == 0) {
            
            self.isBlank = YES;
            
        }
        [_WeakSelf.tableView reloadData];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUDForView:_WeakSelf.view animated:YES];
            
        });
    }];
    
    //下啦刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [_WeakSelf.tableView reloadData];
        
        [_WeakSelf.tableView.mj_header endRefreshing];
        
    }];
    
    //上拉加载
    [self footerRefesh];
    
    //获取当前接口请求到的model数组
    self.modelArray = [[CategoryHelper sharedManage] returnModelArray];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //将页数置为1,下次加载此页面时从第一页开始
    i = 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//上拉加载
-(void)footerRefesh
{
    self.tableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        //判断页数是否越界
        if (i+1 <= [[CategoryHelper sharedManage] categoryModel].totalpage) {
            //根据相关属性获取数据
            [[CategoryHelper sharedManage] fetchModelWithEffectOrCategory:self.categoryOrEffect Page:i+1 Title:self.titleString block:^{
                [_WeakSelf.tableView reloadData];
                //上拉一次增加一页
                i++;
                [_WeakSelf.tableView.mj_footer endRefreshing];
            }];
        }else
        {
            [_WeakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.isBlank) {
        return 1;
    }else
    return self.modelArray.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CategoryDetailViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    // Configure the cell...
    if (self.isBlank) {
        cell.textLabel.text = [NSString stringWithFormat:@"没有找到关于 \"%@\" 的内容",self.titleString];
    }else
    cell.model = self.modelArray[indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WebViewController * webVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"WebVC"];
    webVC.model = self.modelArray[indexPath.row];
    [self.navigationController pushViewController:webVC animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
